<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="120px" @keyup.enter.native="handleQuery">
                <el-form-item label="handle" prop="expert_name">
                    <el-input v-model.trim="queryParams.expert_name" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="平台" prop="platform_id">
                    <el-select v-model="queryParams.platform_id" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in platformType" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="合作店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="建联人" prop="fb_admin_id">
                    <SelectPage
                        v-model="queryParams.fb_admin_id"
                        placeholder="请选择"
                        :url="$api.purchase.getPurchaserList"
                        show_field="username"
                        show_id="operater_id"
                        query_field="keyword"
                        filterable
                        clearable
                        popper-class="custom-select"
                        style="width: 240px"
                    ></SelectPage>
                </el-form-item>
                <el-form-item label="合作级别" prop="level">
                    <el-select v-model="queryParams.level" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in COOPERATION_LEVEL" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="标签" prop="tags">
                    <el-input v-model.trim="queryParams.tags" placeholder="请输入" clearable size="small" style="width: 240px" />
                </el-form-item>
                <!-- <el-form-item label="是否已合作" prop="is_cooperate">
                    <el-select v-model="queryParams.is_cooperate" placeholder="请选择" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in cooperateStatus" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="创建时间" prop="create_time">
                    <el-date-picker
                        v-model="queryParams.create_time"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        style="width: 240px"
                    ></el-date-picker>
                </el-form-item> -->
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" @click="showDetailDialog({})">新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="达人ID" prop="expert_id" min-width="100"></el-table-column>
                <el-table-column label="handle" prop="expert_name" min-width="160">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="gotoPersonalDetail(scope.row)">{{ scope.row.expert_name }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="平台" prop="platform_id" min-width="100">
                    <template slot-scope="scope" v-if="scope.row.platformIdList && scope.row.platformIdList.length">
                      <div v-for="(item, index) in scope.row.platformIdList" :key="index"> 
                         <el-image style="width: 60px; cursor: pointer;margin-top: 4px;" :src="item | formatLabelByValueFuzzy(platformType, {label: 'icon'})" @click="toNewPage(scope.row.link[index])"></el-image>
                      </div>
                    </template>
                </el-table-column>
                <el-table-column label="负责人" prop="operater" min-width="120"></el-table-column>
                <el-table-column label="邮箱" prop="email" min-width="160"></el-table-column>
                <el-table-column label="电话" prop="kol_phone" min-width="100"></el-table-column>
                <el-table-column label="店铺" prop="shop_name" min-width="100"></el-table-column>
                <el-table-column label="合作价格" prop="cooperate_price" min-width="100"></el-table-column>
                <el-table-column label="合作次数" prop="cooperate_nums" min-width="100">
                    <template slot-scope="scope">
                        <el-link type="primary" @click="showCooperateDetailDialog(scope.row)">{{ scope.row.cooperate_nums }}</el-link>
                    </template>
                </el-table-column>
                <el-table-column label="品类" prop="cooperate_spu" min-width="160"></el-table-column>
                <el-table-column label="标签" prop="tags" min-width="100"></el-table-column>
                <el-table-column label="级别" prop="level" min-width="160">
                    <template slot-scope="scope">
                        {{ scope.row.level | formatLabelByValueFuzzy(COOPERATION_LEVEL) }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="创建时间" prop="create_time" min-width="160"></el-table-column> -->
                <el-table-column label="操作" min-width="160" fixed="right">
                    <template slot-scope="scope">
                        <div>
                            <el-button type="text" icon="el-icon-edit" style="padding: 4px 0" @click="showDetailDialog(scope.row, 'edit')">编辑</el-button>
                        </div>
                        <div>
                            <el-button type="text" icon="el-icon-c-scale-to-original" style="padding: 4px 0" @click="showTagsDialog(scope.row)">标签</el-button>
                        </div>
                        <div>
                            <el-button type="text" icon="el-icon-delete" style="padding: 4px 0" @click="deleteRow(scope.row)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" @success="handleQuery"></detail-dialog>
        <cooperate-detail-dialog v-model="cooperateDetailDialog.visible" v-if="cooperateDetailDialog.visible" :expert_id="cooperateDetailDialog.data.expert_id" @success="handleQuery"></cooperate-detail-dialog>
        <tags-dialog v-model="tagDialog.visible" v-if="tagDialog.visible" :params="tagDialog.data" @success="handleQuery"></tags-dialog>
    </div>
</template>

<script>
import { getExpertList, getShop, delExpert } from '@/api/expert'
import { APPROVAL_STATUS, TABS_LIST, COOPERATION_TYPE, COOPERATE_STATUS, EXCELLENT_STATUS, PLATFORM_TYPE, COOPERATION_MODE, COOPERATION_LEVEL} from '../constant'
import DetailDialog from './components/DetailDialog.vue'
import CooperateDetailDialog from './components/CooperateDetailDialog.vue'
import TagsDialog from './components/TagsDialog.vue'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'ShopList',
    components: { DetailDialog, CooperateDetailDialog, TagsDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            // 建联人
            salesmanList: [],
            // 审批结果
            approvalStatus: APPROVAL_STATUS,
            // 合作模式
            cooperateType: COOPERATION_TYPE,
            // 合作状态
            cooperateStatus: COOPERATE_STATUS,
            // 是否优质达人
            excellentStatus: EXCELLENT_STATUS,
            // 平台
            platformType: PLATFORM_TYPE,
            tabsList: TABS_LIST,
            cooperateMode: COOPERATION_MODE,
            COOPERATION_LEVEL,
            shopList: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                expert_name: undefined,
                fb_admin_id: undefined,
                tags: undefined,
                shop_id: undefined,
                is_cooperate: undefined,
                platform_id: undefined,
                level: undefined,
                create_time: [],
            },
            detailDialog: {
                visible: false,
                data: {},
            },
            tagDialog: {
                visible: false,
                data: {},
            },
            cooperateDetailDialog: {
                visible: false,
                data: {},
            }
        }
    },
    async created() {
        this.loading = true
        try {
            const result = await getShop()
            this.shopList = result?.data ?? []
        } catch (error) {
            console.log(error)
            this.loading = false
        }
        this.getList()
    },
    methods: {
        toNewPage(url) {
            window.open(url, '_blank')
        },
        gotoPersonalDetail({expert_id}) {
            this.$router.push({
                path: '/kol/expertView',
                query: {
                    expert_id,
                },
            })
        },
        deleteRow(row) {
            this.$confirm('是否确认删除该数据?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    delExpert({ id: row.id }).then(res => {
                        this.$message.success('删除成功')
                        this.handleQuery()
                    })
                })
                .catch(() => {})
        },
        showTagsDialog(row) {

            this.tagDialog.data = row
            this.tagDialog.visible = true
        },
        showDetailDialog(row) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
        },
        showCooperateDetailDialog(row) {
            this.cooperateDetailDialog.visible = true
            this.cooperateDetailDialog.data = row
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.create_time?.length == 2) {
                qyparams.create_time = [parseTime(qyparams.create_time[0], 'y-m-d'), parseTime(qyparams.create_time[1], 'y-m-d')]
            }
            // if (qyparams.cooperate_time?.length == 2) {
            //     qyparams.cooperate_time = [parseTime(qyparams.cooperate_time[0], 'y-m-d'), parseTime(qyparams.cooperate_time[1], 'y-m-d')]
            // }
            getExpertList(qyparams)
                .then(response => {
                    this.tableData = response.data.list.map(item=>{
                        item.platformIdList = item?.platform_id?.split(',')??[]
                        return {...item}
                    })
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
<style>
.ellipsis-2 .el-link--inner {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
</style>
<style scoped>
.ellipsis-2 {
    width: 100%;
    overflow: hidden;
    word-wrap: break-word;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.el-tabs--border-card {
    box-shadow: none;
    border-bottom: 0;
}

::v-deep .el-tabs .el-tabs__content {
    padding: 0;
    border-bottom: 0;
}
.table-wrap {
    padding: 15px;
    border-bottom: 1px solid #dfe4ed;
    border-left: 1px solid #dfe4ed;
    border-right: 1px solid #dfe4ed;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.12), 0 0 6px 0 rgba(0, 0, 0, 0.04);
}
</style>
